package com.yanyun.yczyy.api.controller;

import com.yanyun.yczyy.api.controller.vo.RealVideoView;
import com.yanyun.yczyy.api.entities.EnergyUsedView;
import com.yanyun.yczyy.api.entities.TblIOData;
import com.yanyun.yczyy.api.repository.IElevatorDataRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
public class RealVideoController {
    @CrossOrigin(origins = {"*"})
    @GetMapping("/platform-api/real-video")
    public RealVideoView realVideo() throws Exception{
        TblIOData tblIOData = elevatorDataRepository.LastElevatorData();
        if (tblIOData == null)
            return RealVideoView.empty();
        Date ioTime = tblIOData.parseIOTime();
        if (System.currentTimeMillis() - ioTime.getTime() > TimeUnit.SECONDS.toMillis(5)){
            return RealVideoView.empty();
        }
        return RealVideoView.from(tblIOData);
    }

    @Autowired
    IElevatorDataRepository elevatorDataRepository;
}
